#include"core.h"
#include<string.h>


static struct csr *s_csr = NULL;

void csr_init(struct csr *csr)
{
    s_csr = csr;
    memset(csr,0,sizeof(struct csr));
}

uint32_t export_read(uint32_t addr)
{
    return csr_read(s_csr,addr);
}

void export_write(uint32_t addr,uint32_t data)
{
    csr_write(s_csr,addr,data);
}

/**
 * @brief 读指定csr寄存器
 * @param csr 寄存器列表
 * @param csr_addr 具体寄存器地址
 */
uint32_t csr_read(struct csr *csr,uint32_t csr_addr){
    switch(csr_addr){
        case CSR_MSTATUS:
            return csr->mstatus;
        case CSR_MIE:
            return csr->mie;
        case CSR_MTVEC:
            return csr->mtvec;
        case CSR_MSCRATCH:
            return csr->mscratch;
        case CSR_MEPC:
            return csr->mepc;
        case CSR_MCAUSE:
            return csr->mcause;
        case CSR_MIP:
            return csr->mip;
        case CSR_MHARTID:
            return csr->mhartid;
        default:
            return 0;
    }
}


/**
 * @brief 写指定csr寄存器
 * @param csr 寄存器列表
 * @param csr_addr 具体寄存器地址
 * @param data 写入数据
 */
int csr_write(struct csr *csr,uint32_t csr_addr,uint32_t data){
    switch(csr_addr){
        case CSR_MSTATUS:
            csr->mstatus = data;
            break;
        case CSR_MIE:
            csr->mie = data;
            break;
        case CSR_MTVEC:
            csr->mtvec = data;
            break;
        case CSR_MSCRATCH:
            csr->mscratch = data;
            break;
        case CSR_MEPC:
            csr->mepc = data;
            break;
        case CSR_MCAUSE:
            csr->mcause = data;
            break;
        case CSR_MIP:
            csr->mip = data;
            break;
        case CSR_MHARTID:
            csr->mhartid = data;
            break;
        default:
            return -1;
    }
    return 0;
}
